package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starsea.book.databinding.ItemBookCityAllBinding
import com.starsea.book.ui.newpage.entity.BookInfoEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage

/**
 * @author Lee
 * @createTime 2024-07-12 20 GMT+8
 * @desc :
 */
class BookCityListAdapter(var mContext: Context) :
    BaseQuickAdapter<BookInfoEntity, BookCityListAdapter.MyViewHolder>() {

    override fun onBindViewHolder(holder: MyViewHolder, position: Int, item: BookInfoEntity?) {

//        holder.binding.ivImage.loadNormalImage(mContext, item!!.bookCover ?: "")
        Glide.with(mContext)
            .asBitmap()
            .load(item!!.bookCover)
            .into(object : CustomViewTarget<ImageView, Bitmap>(holder.binding.ivImage) {
                override fun onLoadFailed(errorDrawable: Drawable?) {
                }

                override fun onResourceCleared(placeholder: Drawable?) {
                }

                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {

                    val height = resource.height
                    val width = resource.width

                    val layoutParams =
                        holder.binding.ivImage.layoutParams as FrameLayout.LayoutParams

                    layoutParams.width =
                        ((ScreenUtils.getScreenWidth() - SizeUtils.dp2px(24f)) / 2.0).toInt()
//
                    layoutParams.height =
                        ((layoutParams.width) * height * 1.0 / width * 1.0).toInt()

                    holder.binding.ivImage.layoutParams = layoutParams

                    holder.binding.ivImage.setImageBitmap(resource)
                }

            })

        holder.binding.tvTitle.text = item.bookTitle
        holder.binding.tvContent.text = item.bookDesc

    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int,
    ): MyViewHolder {
        return MyViewHolder(parent = parent)
    }

    class MyViewHolder(
        parent: ViewGroup,
        val binding: ItemBookCityAllBinding = ItemBookCityAllBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

}